package com.zyc.app.woa.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.IBinder;
import android.os.StatFs;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.google.gson.Gson;
import com.zyc.app.woa.R;
import com.zyc.app.woa.WoaApp;

import java.io.File;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * Author: yingchang.zhang
 * Date  : 2014/12/15
 */
public class CommonUtils {
    public static final long MOUTH_MILLIS = TimeUnit.DAYS.toMillis(30);
    public static final long WEEK_MILLIS = TimeUnit.DAYS.toMillis(7);
    public static final long DAY_MILLIS = TimeUnit.DAYS.toMillis(1);
    public static final long HOUR_MILLIS = TimeUnit.HOURS.toMillis(1);
    public static final long MINUTE_MILLIS = TimeUnit.MINUTES.toMillis(1);
    public static final long SECOND_MILLIS = TimeUnit.SECONDS.toMillis(1);


    private static Gson sGson = null;
    private static String sAppVersion;

    public static Gson getGson() {
        if (null == sGson) {
            sGson = new Gson();
        }
        return sGson;
    }

    public static boolean isWifiConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                WoaApp.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return (null != networkInfo && ConnectivityManager.TYPE_WIFI == networkInfo.getType());
    }

    public static long getSdCardFreeSpace() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcard = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcard.getPath());
            long blockSize = sf.getBlockSize();
            long availCount = sf.getAvailableBlocks();

            //get free space in kb units
            return availCount * blockSize;
        }
        return 0;
    }

    public static String getAppVersion() {
        if (TextUtils.isEmpty(sAppVersion)) {
            String version = "1.0.0";
            PackageManager packageManager = WoaApp.getApp().getPackageManager();
            try {
                PackageInfo info = packageManager.getPackageInfo(WoaApp.getApp().getPackageName(), 0);
                version = info.versionName;
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
            sAppVersion = version;
        }
        return sAppVersion;
    }

    public static void hideSoftInputWindow(IBinder iBinder) {
        InputMethodManager imm =
                (InputMethodManager) WoaApp.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null != imm) {
            imm.hideSoftInputFromWindow(iBinder, 0);
        }
    }

    public static void toast(String msgRes) {
        Toast.makeText(WoaApp.getApp(), msgRes, Toast.LENGTH_SHORT).show();
    }

    public static String getString(int resId) {
        return WoaApp.getApp().getString(resId);
    }

    public static String getString(int resId, Object... params) {
        return WoaApp.getApp().getString(resId, params);
    }

    public static int getColor(int resId) {
        return WoaApp.getApp().getResources().getColor(resId);
    }

    public static String getTimeDiff(long timeMillis) {
        Calendar cal = Calendar.getInstance();
        long diff = 0;
        String str = "";
        diff = System.currentTimeMillis() - timeMillis;

        if (diff > MOUTH_MILLIS) {//30 * 24 * 60 * 60 * 1000=2592000000 毫秒
            str = getString(R.string.mouth_ago);
        } else if (diff > WEEK_MILLIS) {//21 * 24 * 60 * 60 * 1000=1814400000 毫秒
            str = getString(R.string.weeks_ago, (int) Math.floor(diff / WEEK_MILLIS));
        } else if (diff > DAY_MILLIS) {    //24 * 60 * 60 * 1000=86400000 毫秒
            str = getString(R.string.days_ago, (int) Math.floor(diff / DAY_MILLIS));
        } else if (diff > HOUR_MILLIS) {//5 * 60 * 60 * 1000=18000000 毫秒
            str = getString(R.string.hours_ago, (int) Math.floor(diff / HOUR_MILLIS));
        } else if (diff > MINUTE_MILLIS) {//1 * 60 * 1000=60000 毫秒
            str = getString(R.string.minutes_ago, (int) Math.floor(diff / MINUTE_MILLIS));
        } else {
            str = getString(R.string.just_now);
        }
        return str;
    }
}
